package GameJFrame;

import LoginJFrame.Login;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class Game extends JFrame implements KeyListener, ActionListener {

    int[][] imaArr = new int[4][4];
    int y;
    int x;
    int[][] winner = {
            {0, 1, 2, 3},
            {4, 5, 6, 7},
            {8, 9, 10, 11},
            {12, 13, 14, 15}
    };

    String imgFile = "老婆";


    JMenuItem jMenuItem4 = new JMenuItem("妮露");
    JMenuItem jMenuItem5 = new JMenuItem("温迪");
    JMenuItem jMenuItem6 = new JMenuItem("纳西妲");
    JMenuItem jMenuItem7 = new JMenuItem("老婆");
    JMenuItem jMenuItem8 = new JMenuItem("花火");

    JMenuItem jMenuItem1 = new JMenuItem("重新游戏");
    JMenuItem jMenuItem2 = new JMenuItem("重新登陆");
    JMenuItem jMenuItem3 = new JMenuItem("关闭游戏");
    JMenuItem jMenuItem = new JMenuItem("公众号");

    int step = 0;


    public Game() {
        getJFrame();

        JMBar();

        getData();

        addImage();


        this.setVisible(true);
    }

    private void addImage() {
        this.getContentPane().removeAll();

        if (ifWin()) {
            ImageIcon imageIcon = new ImageIcon("PuzzleGame\\image\\win.png");
            JLabel jLabel = new JLabel(imageIcon);
            jLabel.setBounds(203, 283, 197, 73);
            this.add(jLabel);
        }

        for (int i = 0; i < imaArr.length; i++) {
            for (int j = 0; j < imaArr[i].length; j++) {
                ImageIcon imageIcon = new ImageIcon("PuzzleGame\\image\\" + imgFile + "\\0\\" + imaArr[i][j] + ".jpg");
                JLabel jLabel = new JLabel(imageIcon);
                jLabel.setBounds(j * 105 + 83, i * 105 + 135, 105, 105);

                jLabel.setBorder(new BevelBorder(BevelBorder.RAISED));

                this.add(jLabel);
            }
        }

        ImageIcon bg = new ImageIcon("PuzzleGame\\image\\background.png");
        JLabel jLabel1 = new JLabel(bg);
        jLabel1.setBounds(40, 40, 508, 560);
        this.add(jLabel1);

        JLabel num = new JLabel("步数: " + step);
        num.setBounds(50, 30, 100, 20);
        this.add(num);

        this.getContentPane().repaint();
    }

    private void JMBar() {
        JMenuBar jMenuBar = new JMenuBar();
        JMenu jMenu = new JMenu("功能");

        JMenu alterImg = new JMenu("更换图片");
        alterImg.add(jMenuItem4);
        alterImg.add(jMenuItem5);
        alterImg.add(jMenuItem6);
        alterImg.add(jMenuItem7);
        alterImg.add(jMenuItem8);

        jMenu.add(jMenuItem1);
        jMenu.add(jMenuItem2);
        jMenu.add(jMenuItem3);
        jMenu.add(alterImg);
        JMenu jMenu1 = new JMenu("关于我们");


        jMenu1.add(jMenuItem);
        jMenuBar.add(jMenu);
        jMenuBar.add(jMenu1);

        this.setJMenuBar(jMenuBar);


        jMenuItem1.addActionListener(this);//重新游戏
        jMenuItem2.addActionListener(this);//重新登陆
        jMenuItem3.addActionListener(this);//关闭游戏
        jMenuItem.addActionListener(this);//公众号


        jMenuItem4.addActionListener(this);
        jMenuItem5.addActionListener(this);
        jMenuItem6.addActionListener(this);
        jMenuItem7.addActionListener(this);
        jMenuItem8.addActionListener(this);

        this.addKeyListener(this);

    }

    private void getJFrame() {
        this.setSize(603, 680);
        this.setTitle("拼图小游戏");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }

    private void getData() {
        int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int index = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 15) {
                y = i / 4;
                x = i % 4;
            }
            imaArr[i / 4][i % 4] = arr[i];
        }
    }

    private boolean ifWin() {
        for (int i = 0; i < imaArr.length; i++) {
            for (int j = 0; j < imaArr[i].length; j++) {
                if (imaArr[i][j] != winner[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getExtendedKeyCode();
        if (code == 70) {

            this.getContentPane().removeAll();

            ImageIcon imageIcon = new ImageIcon("PuzzleGame\\image\\" + imgFile + "\\0\\all.jpg");
            JLabel jLabel = new JLabel(imageIcon);
            jLabel.setBounds(+83, 135, 420, 420);
            this.add(jLabel);


            ImageIcon bg = new ImageIcon("PuzzleGame\\image\\background.png");
            JLabel jLabel1 = new JLabel(bg);
            jLabel1.setBounds(40, 40, 508, 560);
            this.add(jLabel1);

            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (ifWin()) {
            return;
        }
        int code = e.getExtendedKeyCode();
        if (code == 37) {
            if (x == 3) {
                return;
            }
            int temp = imaArr[y][x];
            imaArr[y][x] = imaArr[y][x + 1];
            imaArr[y][x + 1] = temp;
            x++;
            step++;
            addImage();
        } else if (code == 38) {
            if (y == 3) {
                return;
            }
            int temp = imaArr[y][x];
            imaArr[y][x] = imaArr[y + 1][x];
            imaArr[y + 1][x] = temp;
            y++;
            step++;
            addImage();
        } else if (code == 39) {
            if (x == 0) {
                return;
            }
            int temp = imaArr[y][x];
            imaArr[y][x] = imaArr[y][x - 1];
            imaArr[y][x - 1] = temp;
            x--;
            step++;
            addImage();
        } else if (code == 40) {
            if (y == 0) {
                return;
            }
            int temp = imaArr[y][x];
            imaArr[y][x] = imaArr[y - 1][x];
            imaArr[y - 1][x] = temp;
            y--;
            step++;
            addImage();
        } else if (code == 68) {
            System.out.println("d,一键通关");
            imaArr = new int[][]{
                    {0, 1, 2, 3},
                    {4, 5, 6, 7},
                    {8, 9, 10, 11},
                    {12, 13, 14, 15}
            };
            x = 3;
            y = 3;
            addImage();
        } else if (code == 70) {
            addImage();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jMenuItem1) {
            //重新游戏
            step = 0;
            getData();
            addImage();
        } else if (e.getSource() == jMenuItem2) {
            //重新登陆
            this.setVisible(false);
            new Login();
            this.setVisible(false);
        } else if (e.getSource() == jMenuItem3) {
            //关闭游戏
            System.exit(0);
        } else if (e.getSource() == jMenuItem) {
            //公众号
            System.out.println("公众号");


            JDialog jDialog = new JDialog();
            ImageIcon about = new ImageIcon("\\PuzzleGame\\image\\about.png");
            JLabel jLabel1 = new JLabel(about);
            jLabel1.setBounds(0, 0, 258, 258);
            jDialog.getContentPane().add(jLabel1);
            jDialog.setSize(344, 344);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);


        } else if (e.getSource() == jMenuItem4) {
            imgFile = "妮露";
            step = 0;
            getData();
            addImage();
        } else if (e.getSource() == jMenuItem5) {
            imgFile = "温迪";
            step = 0;
            getData();
            addImage();
        } else if (e.getSource() == jMenuItem6) {
            imgFile = "纳西妲";
            step = 0;
            getData();
            addImage();
        } else if (e.getSource() == jMenuItem7) {
            imgFile = "老婆";
            step = 0;
            getData();
            addImage();
        } else if (e.getSource() == jMenuItem8) {
            imgFile = "花火";
            step = 0;
            getData();
            addImage();
        }
    }
}
